% getssnew.m  (steadystate driver program - short money demand sample)

format long g;


%% PARAMETERS %%%%%%%%%%%

% specify various terms related to fixed cost distribution (cdf, elas, cost
% cutoff, etc.)

phi0 = 0.2; % cdf (hazard rate)
phi1 = 1;
phi2 = 10;
xistar = 0.0015; % fixed cost cutoff
bigxi = 0.0001519;
Fss = phi0;
fss = phi1*phi0/xistar;
dfss = phi2*fss/xistar;

% other parameters
%theta = 1/5.98;
theta = 0.05;
ppsi = 0.50;
sig = 1;
eta = 0.05;
N = 0.2;
b = 0.99;
al = 1/3;
Abar = 1;

% SS solution for taustar, Ftilde
Ftilde = log(phi0);
taustar = log(xistar);

parms = [ppsi sig eta al Abar b theta N Ftilde taustar xistar phi0 bigxi];




%%


%% vector of macro variables [Y chi C bigI K Q in bigxi biglam]

% initial guess for macrov
% K = 2.5;
% Y = 0.6;
K = 3.5;
Y = 0.8;

delta = 0.04;
Q = (1/ppsi)*(xistar*(1-theta)/theta)^theta;
in = (ppsi*Q)^(1/theta);
bigI = in*(K*phi0);
C = Y-K*bigxi-bigI;
chi = ((1-al)*(K^al)*((Abar*N)^-al))/((N^eta)*C);

macrov = [Y; chi; C; bigI; K; Q; in; delta];

recvpnew




%% macrogaps

% initial macro gaps and initilization (for Gauss Newton)
critl=1e-10;
nrm=1;
maxit=100;
macrovnew = macrov;
int_macrov = 1; % dummy (irrelevant!)
[gaps]=macrogapsnew(macrov,parms,int_macrov,critl,maxit); % initial gaps
disp(['Max absolute initial gap = ',num2str(max(abs(gaps)))]);


% Gauss-Newtwon algorithm 

it = 1;

while nrm>critl && it<maxit

    macrov = macrovnew;

    [gaps]=macrogapsnew(macrov,parms,int_macrov,critl,maxit);
    jac=numjac('macrogapsnew',macrov,.000001,parms,int_macrov,critl,maxit);

    macrovnew = macrov-inv(jac)*gaps;
    nrm = max(abs(gaps));

    disp(['Max dx = ',num2str(max(inv(jac)*gaps))])
    disp(['Max absolute gap = ',num2str(nrm)]);

    it=it+1;

    if it==maxit
        disp('Maximum iteration is reached');
    end

end


% recover macrov
recvpnew




